#include "ToolUtils.h"

ToolUtils::ToolUtils() {

}



bool ToolUtils::isPureInteger(const QString& text) {
    QRegularExpression regex("^[-+]?\\d+$");

    QRegularExpressionMatch match = regex.match(text);
    return match.hasMatch();
}

QString ToolUtils::getCurrentTime() {
    QDateTime currentDateTime = QDateTime::currentDateTime();

    QString formattedTime = currentDateTime.toString("yyyy-MM-dd hh:mm:ss");

    return formattedTime;
}

int ToolUtils::getCurrentDayOfWeek() {
    QDate currentDate = QDate::currentDate();

    int dayOfWeekFull = currentDate.dayOfWeek();

    return dayOfWeekFull;
}


QByteArray ToolUtils::CalculateCRC16(QByteArray data)
{

    uint16_t crc = 0xFFFF;
    for (uint16_t i = 0; i < data.size(); i++)
    {
        crc ^= data.at(i);
        for (uint8_t j = 0; j < 8; j++)
        {
            if (crc & 0x0001)
            {
                crc >>= 1;
                crc ^= 0xA001;
            }
            else
            {
                crc >>= 1;
            }
        }
    }

    QByteArray byteArray;

    byteArray.append(1, (uint8_t)(crc >> 8));
    byteArray.append(1, (uint8_t)(crc));

    return byteArray;
}